local LoR_Utility = require "packages/th_jie/LoR_Utility"
local extension = Package:new("kaman")
extension.extensionName = "decade"
Fk:loadTranslationTable{
    ["kaman"] = "假面",
}

local kuuga = General:new(extension, "kaman__kuuga", "god", 4, 5)

-- 武将翻译
Fk:loadTranslationTable{
    ["kaman__kuuga"] = "空我",
    ["#kaman__kuuga"] = "漆黑的天使",
    ["designer:kaman__kuuga"] = "decade",
    ["cv:kaman__kuuga"] = "无",
    ["illustrator:kaman__kuuga"] = "无",
}
-- 创建一个名为 kaman_shaqi 的触发技能
local kaman_shaqi = fk.CreateTriggerSkill {
  -- 技能名称
  name = "kaman_shaqi",
  -- 静音标志，可能用于控制技能触发时的音效等
  mute = true,
  -- 技能频率，这里是锁定技
  frequency = Skill.Compulsory,
  -- 技能触发的事件，回合开始阶段开始和角色死亡时
  events = { fk.EventPhaseStart, fk.Deathed },
  -- 判断技能是否可以触发的函数
  can_trigger = function(self, event, target, player, data)
    -- 若玩家拥有该技能
    if player:hasSkill(self) then
      -- 当回合开始阶段开始且目标是玩家，或者角色死亡且玩家不在非活动阶段时触发
      return (event == fk.EventPhaseStart and player == target and player.phase == Player.Start) or
          (event == fk.Deathed and player.phase ~= Player.NotActive)
    end
  end,
  -- 技能触发时执行的函数
  on_trigger = function(self, event, target, player, data)
    -- 获取玩家所在的房间
    local room = player.room
    -- 遍历房间内所有存活的玩家
    for _, p in ipairs(room.alive_players) do
      -- 若该玩家与当前玩家相邻
      if player:getNextAlive() == p or p:getNextAlive() == player then
        -- 为相邻玩家设置标记，表示在当前玩家回合内其技能失效
        room:setPlayerMark(p, "kaman_shaqi-turn", 1)
      end
    end
  end,
}

-- 创建一个名为 #kaman_shaqi_invalidity 的无效化技能
local kaman_shaqi_invalidity = fk.CreateInvaliditySkill {
  -- 技能名称
  name = "#kaman_shaqi_invalidity",
  -- 判断技能是否无效的函数
  invalidity_func = function(self, from, skill)
    -- 若玩家有 kaman_shaqi-turn 标记，则技能无效
    return from:getMark("kaman_shaqi-turn") > 0
  end
}

-- 将无效化技能关联到 kaman_shaqi 技能上
kaman_shaqi:addRelatedSkill(kaman_shaqi_invalidity)
-- 给武将 kuuga 添加 kaman_shaqi 技能
kuuga:addSkill(kaman_shaqi)
-- 加载技能的翻译表
Fk:loadTranslationTable {
  -- 技能的显示名称
  ["kaman_shaqi"] = "杀气",
  -- 技能的描述
  [":kaman_shaqi"] = "锁定技，与你相邻的角色于你的回合内所有技能失效。",
}
  
-- 创建一个名为 kaman_dianhuo 的触发技能
local kaman_dianhuo = fk.CreateTriggerSkill {
  -- 技能名称
  name = "kaman_dianhuo",
  -- 技能提示信息
  prompt = "#kaman_dianhuo",
  -- 技能动画类型
  anim_type = "special",
  -- 技能触发的事件，目标指定时
  events = { fk.TargetSpecifying },
  -- 判断技能是否可以触发的函数
  can_trigger = function(self, event, target, player, data)
    -- 当目标是玩家，玩家拥有该技能，使用的是伤害牌，目标不是玩家自己，且目标没有烧伤标记时触发
    return target == player and player:hasSkill(self) and data.card and data.card.is_damage_card == true and
        data.to ~= player.id and data.to
        and player.room:getPlayerById(data.to):getMark("@kaman_fire") == 0
  end,
  -- 技能触发时执行的函数
  on_use = function(self, event, target, player, data)
    -- 获取玩家所在的房间
    local room = player.room
    -- 为目标玩家添加烧伤标记
    room:addPlayerMark(room:getPlayerById(data.to), "@kaman_fire")
   end
}
-- 给武将 kuuga 添加 kaman_dianhuo 技能
kuuga:addSkill(kaman_dianhuo)
-- 加载技能的翻译表
Fk:loadTranslationTable {
  -- 技能的显示名称
  ["kaman_dianhuo"] = "点火",
  -- 技能的描述
  [":kaman_dianhuo"] = "锁定技，使用伤害牌指定其他角色后为其附加烧伤。"
}

-- 创建一个名为 kaman__fuzhi 的触发技能
local kaman__fuzhi = fk.CreateTriggerSkill{
  -- 技能名称
  name = "kaman__fuzhi",
  -- 技能动画类型
  anim_type = "defensive",
  -- 技能频率，这里是锁定技
  frequency = Skill.Compulsory,
  -- 技能触发的事件，目标被指定时
  events = {fk.TargetSpecified},
  -- 判断技能是否可以触发的函数
  can_trigger = function(self, event, target, player, data)
    -- 当玩家拥有该技能，目标不是玩家，使用的是杀或锦囊牌，且玩家在目标列表中时触发
    return player:hasSkill(self.name) and target ~= player and 
           (data.card.trueName == "slash" or data.card.type == Card.TypeTrick) and 
           data.firstTarget and table.contains(AimGroup:getAllTargets(data.tos), player.id)
  end,
  -- 技能触发时执行的函数
  on_use = function(self, event, target, player, data)
    -- 获取玩家所在的房间
    local room = player.room
    -- 获取使用的卡牌
    local card = data.card
    -- 若卡牌不存在则返回 false
    if not card then return false end
    -- 用于存储可复制的技能列表
    local skills = {}
    -- 遍历目标玩家的所有技能
    for _, s in ipairs(target.player_skills) do
      -- 筛选出可见、非隐藏技能、非特定频率技能且玩家未拥有的技能
      if s.visible and not s.name:startsWith("#") and not s.name:endsWith("&")
          and not table.contains({ Skill.Limited, Skill.Lord }, s.frequency)
          and not player:hasSkill(s.name) then
        -- 将符合条件的技能添加到可复制技能列表中
        table.insert(skills, s.name)
      end
    end

    -- 若有可复制的技能
    if #skills > 0 then
      -- 随机选择一个技能进行复制
      local skill_to_copy = skills[math.random(1, #skills)]  
      -- 处理玩家添加技能的操作
      room:handleAddLoseSkills(player, skill_to_copy, nil)
      -- 发送日志信息，记录复制技能的操作
      room:sendLog{
        type = "#fuzhiCopySkill",
        from = player.id,
        to = {target.id},
        arg = skill_to_copy,
      }
    end
  end

}

kuuga:addSkill(kaman__fuzhi)
Fk:loadTranslationTable{
  ["kaman__fuzhi"] = "复制",

  [":kaman__fuzhi"] = "锁定技，你的回合外，当其他角色对你使用牌时,你复制其一个技能。",
}


--究极体
local jiujiti = fk.CreateTriggerSkill{
  name = "kaman_jiujiti",
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  events = {fk.DamageCaused,fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self.name) and (
      (event == fk.DamageCaused and target == player) or -- 造成伤害
      (event == fk.DamageInflicted and target == player) -- 受到伤害
    )
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    -- 添加究极体变身动画（2秒多动画）
    room:doSuperLightBox("packages/decade/qml/xuli.qml")  -- 伤害翻倍专属动画
    room:delay(1300)  -- 调整为2.5秒同步动画时长
 
    
    if event == fk.DamageCaused then
      -- 先处理属性伤害免疫
      if data.damageType ~= fk.NormalDamage then
        data.damage = 0
      else
        -- 普通伤害翻倍
        data.damage = data.damage * 2
      end
    elseif event == fk.DamageInflicted then
      -- 受到伤害减半
      data.damage = math.ceil(data.damage / 2)
      end
  
  end,
}
kuuga:addSkill(jiujiti)
Fk:loadTranslationTable{
  ["kaman_jiujiti"] = "究极体",
  [":kaman_jiujiti"] = "锁定技，你造成的伤害翻倍，免疫属性伤害，受到伤害减半（向上取整）。",
}













--状态技能：烧伤
local kaman_fire = fk.CreateTriggerSkill {
  name = "kaman_fire",
  anim_type="kaman_fire",
  global = true,
  priority = 2,
  frequency = Skill.Compulsory,
  events = { fk.TurnEnd },
  can_trigger = function(self, event, target, player, data)
    return player:getMark("@kaman_fire") > 0 and not player.dead
  end,
  on_use = function(self, event, target, player, data)
    local num = player:getMark("@kaman_fire")
    player.room:setPlayerMark(player, "@kaman_fire", 0)
    player.room:sendLog({ type = "#kaman_fire_Log", from = player.id,arg=num})
    player.room:damage({
      to = player,
      damage = num,
      damageType = fk.FireDamage,
      skillName=self.name,
    })
  end,
}
Fk:addSkill(kaman_fire)
Fk:loadTranslationTable {
  ["kaman_fire"] = "烧伤",
  ["kaman_fire_jieshao"] = "<br><font color='grey'><b>#烧伤</b><br>目标在当前回合结束时失去全部【烧伤】并受到等同于【烧伤】数量的无来源火焰伤害",
  ["@kaman_fire"] = "烧伤",
  ["#kaman_fire_Log"] = "%from因【烧伤】效果，受到%arg点火焰伤害",
}



local joker = General:new(extension, "kaman__joker", "god", 4, 5)
-- 武将翻译
Fk:loadTranslationTable{
  ["kaman__joker"] = "joker",
  ["#kaman__joker"] = "唯心的王牌",
  ["designer:kaman__joker"] = "decade",
  ["cv:kaman__joker"] = "无",
  ["illustrator:kaman__joker"] = "无",}
 
  local kaman_wangpai = fk.CreateTriggerSkill {
    name = "kaman_wangpai",
    anim_type = "offensive",
    frequency = Skill.Compulsory,
    events = { fk.DamageCaused },
    can_trigger = function(self, event, target, player, data)
      return target == player and player:hasSkill(self) and data.card and (data.card.trueName == "slash" or data.card.trueName == "duel")
    end,
    on_use = function(self, event, target, player, data)
      data.damage = data.damage + 1
    end,
  }
  local kaman_wangpai_mod = fk.CreateTargetModSkill {
    name = "#kaman_wangpai_mod",
    frequency = Skill.Compulsory,
    bypass_times = function(self, player, skill, scope)
        if player:hasSkill("kaman_wangpai") and 
            (skill.trueName == "slash_skill" or skill.trueName == "duel_skill") and  -- 添加决斗限制
            scope == Player.HistoryPhase then
            return 999
        end
    end,
}

local kaman_wangpai_recover = fk.CreateTriggerSkill{
    name = "#kaman_wangpai_recover",
    events = {fk.Damaged},
    frequency = Skill.Compulsory,
    can_trigger = function(self, event, target, player, data)
        return player == target and player:hasSkill("kaman_wangpai")
    end,
    on_trigger = function(self, event, target, player, data)
        local room = player.room
        local total = player:getMark("kaman_wangpai_damage") + data.damage
        while total >= 3 do  -- 使用while处理单次大额伤害
            room:recover({
                who = player,
                num = 1,
                skillName = self.name,
            })
            total = total - 3
        end
        room:setPlayerMark(player, "kaman_wangpai_damage", total)
    end
}

-- 新增回复摸牌技能 --
local kaman_wangpai_draw = fk.CreateTriggerSkill{
  name = "#kaman_wangpai_draw",
  events = {fk.HpChanged},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return player == target and player:hasSkill("kaman_wangpai")
  end,
  on_use = function(self, event, target, player, data)
      local lostHp = player.maxHp - player.hp
      if lostHp > 0 then
          player.room:drawCards(player, lostHp, self.name)
      elseif event == fk.CardFinished then
      if data.card and data.card.trueName == "slash" then
        for _, p in ipairs(room.alive_players) do
          room:setPlayerMark(p, "#wushen_wuren-invalid_"..currentCardId, 0)
        end
      end
    end
  end
}
joker:addSkill(kaman_wangpai)
kaman_wangpai:addRelatedSkill(kaman_wangpai_mod)
kaman_wangpai:addRelatedSkill(kaman_wangpai_recover)
kaman_wangpai:addRelatedSkill(kaman_wangpai_draw)
Fk:loadTranslationTable {
    ["kaman_wangpai"] = "王牌",
    ["kaman_wangpai_mod"] = "王牌",
    ["kaman_wangpai_recover"] = "王牌",
    ["kaman_wangpai_draw"] = "王牌",
    [":kaman_wangpai"] = "锁定技，你使用【杀】无次数限制；你的【杀】和【决斗】伤害+1；体力变化时摸已损体力的牌，你每累计受到3点伤害回复1点体力；",
}

local taili = General:new(extension, "kaman__taili", "god", 4, 4)


Fk:loadTranslationTable{
  ["kaman__taili"] = "武神之刃",
  ["#kaman__taili"] = "漆黑的将军",
  ["designer:kaman__taili"] = "decade",
  ["cv:kaman__taili"] = "无",
  ["illustrator:kaman__taili"] = "无",
}

-- 武刃技能
local kaman_wuren = fk.CreateTriggerSkill{
  name = "kaman_wuren",
  frequency = Skill.Compulsory,
  events = {fk.DamageCaused, fk.TargetSpecifying},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self.name) then return false end
    
    if event == fk.DamageCaused then
      return target == player and data.card and data.card.trueName == "slash" and not data.wuren_added
    elseif event == fk.TargetSpecifying then
      return target == player and data.card and data.card.trueName == "slash"
    end
    return false
  end,
  
  on_use = function(self, event, target, player, data)
      local room = player.room
    if event == fk.DamageCaused then
      -- 使用【杀】造成伤害+1
      data.damage = data.damage + 1
      data.wuren_added = true
    elseif event == fk.TargetSpecifying then
      -- 使用【杀】无视防具
      if data.to then
        local to = room:getPlayerById(data.to)
        if to then
          local use_event = room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
          if use_event then
            -- 无视防具效果
            room:addPlayerMark(to, fk.MarkArmorNullified)
            use_event:addCleaner(function()
              room:removePlayerMark(to, fk.MarkArmorNullified)
            end)
            -- 技能失效效果
            room:setPlayerMark(to, "kaman_wuren-turn", 1)
          end
        end
      end
    end
  end,
}

-- 创建一个无效化技能
local kaman_wuren_invalidity = fk.CreateInvaliditySkill{
  name = "#kaman_wuren_invalidity",
  invalidity_func = function(self, from, skill)
    return from:getMark("kaman_wuren-turn") > 0
  end
}

-- 将无效化技能关联到武刃技能上
kaman_wuren:addRelatedSkill(kaman_wuren_invalidity)

-- 添加回合结束时清除标记的效果
local kaman_wuren_clear = fk.CreateTriggerSkill{
  name = "#kaman_wuren_clear",
  frequency = Skill.Compulsory,
  events = {fk.EventPhaseEnd},
  can_trigger = function(self, event, target, player, data)
    return target.phase == Player.NotActive
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room.alive_players) do
      if p:getMark("kaman_wuren-turn") > 0 then
        room:setPlayerMark(p, "kaman_wuren-turn", 0)
      end
    end
  end,
}

kaman_wuren:addRelatedSkill(kaman_wuren_clear)

-- 武刃无次数限制
local wuren_mod = fk.CreateTargetModSkill{
  name = "#kaman_wuren_mod",
  residue_func = function(self, player, skill, scope, card)
    if player:hasSkill("kaman_wuren") and skill.trueName == "slash_skill" and scope == Player.HistoryPhase then
      return 999
    end
  end,
}

kaman_wuren:addRelatedSkill(wuren_mod)

taili:addSkill(kaman_wuren)

-- 技能翻译
Fk:loadTranslationTable{
  ["kaman_wuren"] = "武刃",
  [":kaman_wuren"] = "锁定技，<br/>①你使用【杀】无次数限制，<br/>②你使用【杀】造成伤害+1，<br/>③你使用【杀】无视防具，<br/>④你使用【杀】指定目标后，令其技能失效直到回合结束。",
}

-- 黑甲技能
local kaman_heijia = fk.CreateTriggerSkill{
  name = "kaman_heijia",
  frequency = Skill.Compulsory,
  events = {fk.DamageInflicted, fk.PreHpLost, fk.BeforeMaxHpChanged},
  
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self.name) then return false end
    
    if event == fk.DamageInflicted then
      return target == player
    elseif event == fk.PreHpLost then
      return target == player
    elseif event == fk.BeforeMaxHpChanged then
      return target == player and data.num < 0
    end
    return false
  end,
  
  on_use = function(self, event, target, player, data)
    local room = player.room
    
    if event == fk.DamageInflicted then
  
      -- 免疫低于两点伤害
      if data.damage <= 1 then
        return true  -- 返回true表示取消此伤害
      end
      -- 高于两点伤害减半
      if data.damage > 1 then
        data.damage = math.ceil(data.damage / 2)
      end
    elseif event == fk.PreHpLost then
      -- 免疫体力流失
      return true
    elseif event == fk.BeforeMaxHpChanged and data.num < 0 then
      -- 免疫体力上限减少
      return true
    end
  end,
}

taili:addSkill(kaman_heijia)

Fk:loadTranslationTable{
  ["kaman_heijia"] = "黑甲",
  [":kaman_heijia"] = "锁定技，①你免疫小于或等于1点的伤害。②你免疫体力流失和体力上限减少。③你受到大于1点的伤害时，伤害值减半(向上取整)。",
}

-- 半月斩技能
local kaman_banyuezhan = fk.CreateActiveSkill{
  name = "kaman_banyuezhan",
  anim_type = "offensive",
  can_use = function(self, player)
    return true
  end,
  card_filter = function(self, card)
    return true
  end,
  card_num = 2,
  target_num = 0,
  card_num_min = 2,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local cards = table.map(effect.cards, function(id)
      return Fk:getCardById(id)
    end)
    if #cards ~= 2 or cards[1].suit == cards[2].suit then return false end
     -- 播放血月斩动画
     room:doSuperLightBox("packages/decade/qml/xueyuezhan.qml")
     room:delay(5000)  -- 假设动画时长为4秒，可根据实际情况调整
    -- 弃置两张牌
    room:throwCard(effect.cards, self.name, player, player)
    room:setPlayerMark(player, "banyuezhan_buff", 1)
  end,
}

local kaman_banyuezhan_buff = fk.CreateTriggerSkill{
  name = "#kaman_banyuezhan_buff",
  mute = true,
  frequency = Skill.Compulsory,
  events = {fk.AfterCardTargetDeclared, fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill("kaman_banyuezhan") then return false end
    
    if event == fk.AfterCardTargetDeclared then
      return player:getMark("banyuezhan_buff") > 0 and target == player 
        and data.card and data.card.trueName == "slash"
    elseif event == fk.DamageCaused then
      return player:getMark("banyuezhan_buff") > 0 and target == player 
        and data.card and data.card.trueName == "slash"
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.AfterCardTargetDeclared then
      local targets = TargetGroup:getRealTargets(data.tos)
      for _, p in ipairs(room.alive_players) do
        if p.id ~= player.id and player:inMyAttackRange(p) and 
           not table.contains(targets, p.id) and not player:isProhibited(p, data.card) then
          room:doIndicate(player.id, {p.id})
          TargetGroup:pushTargets(data.tos, p.id)
        end
      end
    elseif event == fk.DamageCaused then
      data.damage = data.damage + 1
      room:setPlayerMark(player, "banyuezhan_buff", 0)
    end
  end
}

kaman_banyuezhan:addRelatedSkill(kaman_banyuezhan_buff)

taili:addSkill(kaman_banyuezhan)

Fk:loadTranslationTable{
  ["kaman_banyuezhan"] = "血月斩",
  [":kaman_banyuezhan"] = "你可以弃置两张不同花色的手牌，增强下一张【杀】，此【杀】伤害+1且必须指定你攻击范围内的所有其他角色为目标。",
}

-- 圆月斩技能
local kaman_yuanyuezhan = fk.CreateActiveSkill{
  name = "kaman_yuanyuezhan",
  anim_type = "offensive",
  can_use = function(self, player)
    return true
  end,
  card_filter = function(self, card)
    return true
  end,
  card_num = 2,
  target_num = 0,
  card_num_min = 2,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local cards = table.map(effect.cards, function(id)
      return Fk:getCardById(id)
    end)
    if #cards ~= 2 or cards[1].suit == cards[2].suit then return false end
    
    -- 播放胜利时刻动画
    room:doSuperLightBox("packages/decade/qml/shenglishike.qml")
    room:delay(4000)  -- 2秒动画时长
    
    -- 弃置两张牌
    room:throwCard(effect.cards, self.name, player, player)
    room:setPlayerMark(player, "yuanyuezhan_buff", 1)
  end,
}

local kaman_yuanyuezhan_buff = fk.CreateTriggerSkill{
  name = "#kaman_yuanyuezhan_buff",
  mute = true,
  frequency = Skill.Compulsory,
  events = {fk.CardUsing, fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill("kaman_yuanyuezhan") then return false end
    
    if event == fk.CardUsing then
      return player:getMark("yuanyuezhan_buff") > 0 and target == player 
        and data.card and data.card.trueName == "slash"
    elseif event == fk.DamageCaused then
      return player:getMark("yuanyuezhan_buff") > 0 and target == player 
        and data.card and data.card.trueName == "slash"
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardUsing then
      data.extra_data = data.extra_data or {}
      data.extra_data.yuanyuezhan = true
    elseif event == fk.DamageCaused then
      data.damage = data.damage + 1
      room:setPlayerMark(player, "yuanyuezhan_buff", 0)
    end
  end,

  refresh_events = {fk.CardUseFinished},
  can_refresh = function(self, event, target, player, data)
    return target == player and data.extra_data and data.extra_data.yuanyuezhan
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    data.extra_data.yuanyuezhan = false
    -- 创建一个新的使用效果
    local newData = {
      from = player.id,
      tos = data.tos,  -- 保持相同的目标
      card = data.card,  -- 使用相同的卡牌
    }
    room:useCard(newData)
  end,
}

kaman_yuanyuezhan:addRelatedSkill(kaman_yuanyuezhan_buff)

taili:addSkill(kaman_yuanyuezhan)

Fk:loadTranslationTable{
  ["kaman_yuanyuezhan"] = "胜利时刻",
  [":kaman_yuanyuezhan"] = "出牌阶段，你可以弃置两张不同花色的手牌，增强下一张【杀】，此【杀】伤害+1且结算次数+1。",
}

-- 和平之剑技能
local kaman_hepingzhijian = fk.CreateTriggerSkill{
  name = "kaman_hepingzhijian",
  anim_type = "offensive",
  events = {fk.AfterSkillEffect},
  
  can_trigger = function(self, event, target, player, data)
    local slash = Fk:cloneCard('slash')
    return player:hasSkill(self) and player.phase == Player.NotActive and
      target and target ~= player and
      player.room.logic:getCurrentEvent().can_hepingzhijian and
      not player:prohibitUse(slash) and
      not player:isProhibited(target, slash)
  end,

  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, data, "#kaman_hepingzhijian-invoke::"..target.id)
  end,

  on_use = function(self, event, target, player, data)
    local room = player.room
    -- 播放一闪动画
    room:doSuperLightBox("packages/decade/qml/jianshan.qml")  -- 一闪专属动画
    room:delay(4000)  -- 4秒动画时长
    
    local slash = Fk:cloneCard('slash')
    slash.skillName = self.name
    room:useCard({
      from = player.id,
      tos = {{target.id}},
      card = slash,
      disresponsiveList = table.map(room.alive_players, Util.IdMapper),
    })
  end,

  refresh_events = {fk.AfterSkillEffect},
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(self) and player.phase == Player.NotActive and
      target and target ~= player and
      target:hasSkill(data) and data.visible and
      not player.room.logic:getCurrentEvent().hepingzhijian_counted
  end,

  on_refresh = function(self, event, target, player, data)
    local room = target.room
    local cur = room.logic:getCurrentEvent()
    cur.hepingzhijian_counted = true
    room:addPlayerMark(target, "@hepingzhijian-turn", 1)
    if (target:getMark("@hepingzhijian-turn") % 5 == 0) then
      cur.can_hepingzhijian = true
    end
  end,
}

taili:addSkill(kaman_hepingzhijian)

Fk:loadTranslationTable{
  ["kaman_hepingzhijian"] = "一闪",
  ["@hepingzhijian-turn"] = "一闪",
  ["#kaman_hepingzhijian-invoke"] = "一闪：你可以视为对 %dest 使用一张不可响应的【杀】",
  [":kaman_hepingzhijian"] = "你的回合外，当其他角色于一回合内每发动五次技能后，你可以视为对其使用一张不可响应的【杀】。",
}
local zio = General:new(extension, "kaman__zio", "god", 4, 4)
zio.total_hidden = true
-- 添加武将翻译
Fk:loadTranslationTable {
  ["kaman__zio"] = "ZIO二阶",
  ["#kaman__zio"] = "时间的王者",
  ["designer:kaman__zio"] = "DECADE",
  ["cv:kaman__zio"] = "无",
  ["illustrator:kaman__zio"] = "无",
}

-- 创建时停技能
local kaman_shiting = fk.CreateTriggerSkill{
  name = "kaman_shiting",
  anim_type = "control",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target.phase == Player.Start
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, data, "#kaman_shiting")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    
    -- 获得一个额外出牌阶段并摸两张牌
    player:gainAnExtraPhase(Player.Play)
    player:drawCards(2, self.name)
    room:setPlayerMark(player, "@@shiting", 1)
  end,
}

-- 时停效果
local kaman_shiting_effect = fk.CreateTriggerSkill{
  name = "#kaman_shiting_effect",
  mute = true,
  events = {fk.TargetSpecifying, fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if event == fk.TargetSpecifying then
      return target == player and player:getMark("@@shiting") > 0
    else
      return target == player and player:getMark("@@shiting") > 0 and player.phase == Player.Play
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TargetSpecifying then
      data.disresponsive = true
    else
      for _, p in ipairs(room.alive_players) do
        if p ~= player then
          -- 使其他角色的所有技能失效
          room:setPlayerMark(p, MarkEnum.UncompulsoryInvalidity .. "-phase", 1)
        end
      end
    end
  end,
}

-- 时停清除效果
local kaman_shiting_clear = fk.CreateTriggerSkill{
  name = "#kaman_shiting_clear",
  mute = true,
  events = {fk.EventPhaseEnd},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:getMark("@@shiting") > 0 and player.phase == Player.Play
  end,
  on_refresh = function(self, event, target, player, data)
      local room = player.room
    room:setPlayerMark(player, "@@shiting", 0)
    -- 清除其他角色的技能失效标记
    for _, p in ipairs(room.alive_players) do
      if p ~= player then
        room:setPlayerMark(p, MarkEnum.UncompulsoryInvalidity .. "-phase", 0)
      end
    end
  end,
}

kaman_shiting:addRelatedSkill(kaman_shiting_effect)
kaman_shiting:addRelatedSkill(kaman_shiting_clear)

-- 创建回溯技能
local kaman_huisu = fk.CreateActiveSkill{
  name = "kaman_huisu",
  anim_type = "control",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 
  end,
  card_num = 0,
  target_num = 1,
  target_filter = function(self, to_select, selected, selected_cards)
    return true
  end,
  feasible = function(self, selected, selected_cards)
    return #selected == 1
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    
    -- 如果目标已死亡，先复活
    if target.dead then
      room:revivePlayer(target, true)
      -- 发送复活提示
      room:sendLog{
        type = "#kaman_huisu_revive",
        from = player.id,
        to = {target.id},
        arg = self.name,
      }
    end
    
    -- 获取所有技能
    local skills = {}
    for _, s in ipairs(target.player_skills) do
      if s.visible and not s.name:startsWith("#") then
        table.insertIfNeed(skills, s.name)
      end
    end
    
    -- 重置目标角色
    if #skills > 0 then
      room:handleAddLoseSkills(target, "-" .. table.concat(skills, "|-"), nil)
    end
    
    -- 恢复武将原始技能
    local general = Fk.generals[target.general]
    if not general then return false end
    
    local num_maxhp = general.maxHp
    local general_skills = general:getSkillNameList()
    if #general_skills > 0 then
      room:handleAddLoseSkills(target, table.concat(general_skills, "|"), nil)
    end
    
    if target.deputyGeneral then
      local deputy = Fk.generals[target.deputyGeneral]
      if deputy then
        num_maxhp = math.floor((num_maxhp + deputy.maxHp) / 2)
        local deputy_skills = deputy:getSkillNameList()
        if #deputy_skills > 0 then
          room:handleAddLoseSkills(target, table.concat(deputy_skills, "|"), nil)
        end
      end
    end
    
    -- 恢复体力上限
    if num_maxhp ~= target.maxHp then
      room:changeMaxHp(target, num_maxhp - target.maxHp)
    end
    
    -- 重置标记
    target:reset()
    
    -- 恢复体力值到体力上限
    if target.hp < target.maxHp then
      room:recover({
        who = target,
        num = target.maxHp - target.hp,
          skillName = self.name
      })
    end

    -- 弃置所有牌并摸到4张
    if not target.dead then
      room:throwAllCards(target, "he", self.name)
      local n = 4 - target:getHandcardNum()
      if n > 0 then
        room:drawCards(target, n, self.name)
      end
    end
  end,
}



-- 添加武将翻译和技能翻译
Fk:loadTranslationTable {
  
  ["kaman_shiting"] = "时停",
  ["#kaman_shiting"] = "时停：获得一个额外的出牌阶段",
  [":kaman_shiting"] = "每名角色的准备阶段开始时，你可以获得一个额外的出牌阶段并摸两张牌。在你的回合内，你使用牌不可被响应且其他角色非锁定技技能失效。",
  ["@@shiting"] = "时停",

  ["kaman_huisu"] = "回溯",
  ["#kaman_huisu"] = "回溯：令一名角色回到初始状态",
  [":kaman_huisu"] = "出牌阶段限一次，你可以选择一名角色，令其回到游戏开始时的状态（包括技能、体力、体力上限等），然后其弃置所有牌并将手牌摸至4张。",
  ["#kaman_huisu_revive"] = "%from 发动了【%arg】令 %to 复活",

  ["kaman_fuhuo"] = "回溯:复活",
  [":kaman_fuhuo"] = "准备阶段，你可以选择一名已阵亡的角色令其复活。",
  ["#kaman_fuhuo-dialog"] = "请选择一名已阵亡的角色令其复活",
  ["#kaman_fuhuo_log"] = "%from 发动了【%arg】令 %to 复活",

  ["kaman_yuzhi"] = "预知",
  [":kaman_yuzhi"] = "当你成为【杀】或普通锦囊牌的目标后，你可以选择：1.失去1点体力上限；2.失去1点体力。然后取消此牌对你的目标，摸两张牌并对来源使用一张不可被响应的【杀】。",
  ["#kaman_yuzhi-choice"] = "预知：你成为了 %arg 使用的 %arg2 的目标，请选择：1.失去1点体力上限；2.失去1点体力",
  ["lose_maxhp"] = "失去体力上限",
  ["lose_hp"] = "失去体力",
}

-- 创建复活技能
local kaman_fuhuo = fk.CreateTriggerSkill{
  name = "kaman_fuhuo",
  anim_type = "control",
  events = {fk.EventPhaseStart},
  
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self.name) and player.phase == Player.Start and
           #table.map(table.filter(player.room.players, function(p) return p.dead end), function(p) return p.general end) > 0
  end,

  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, data, "#kaman_fuhuo-dialog")
  end,

  on_use = function(self, event, target, player, data)
      local room = player.room
    player:broadcastSkillInvoke(self.name)
    room:notifySkillInvoked(player, self.name, "control")
    
    -- 获取所有死亡角色
    local targets = table.map(table.filter(room.players, function(p) return p.dead end), function(p) return p.general end)
    
    -- 选择一个死亡角色
    local choice = room:askForChoice(player, targets, self.name, "#kaman_fuhuo-dialog")
    
    if choice and #choice > 0 then
      -- 找到并复活目标角色
      for _, p in ipairs(room.players) do
        if p.general == choice then
          -- 复活角色
          room:revivePlayer(p, true)
          -- 发送日志
          room:sendLog{
            type = "#kaman_fuhuo_log",
            from = player.id,
            to = {p.id},
            arg = self.name
          }
          break
        end
      end
    end
  end,
}

-- 添加技能翻译
Fk:loadTranslationTable {
  ["kaman_fuhuo"] = "回溯:复活",
  [":kaman_fuhuo"] = "准备阶段，你可以选择一名已阵亡的角色令其复活。",
  ["#kaman_fuhuo-dialog"] = "请选择一名已阵亡的角色令其复活",
  ["#kaman_fuhuo_log"] = "%from 发动了【%arg】令 %to 复活",
}
-- 创建预知技能
local kaman_yuzhi = fk.CreateTriggerSkill{
  name = "kaman_yuzhi",
  anim_type = "defensive",
  events = {fk.TargetConfirmed},
  
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self.name) and 
           (data.card:isCommonTrick() or data.card.trueName == "slash")
  end,

  on_cost = function(self, event, target, player, data)
      local room = player.room
    local choices = {"lose_maxhp"}
    if player.hp > 1 then table.insert(choices, "lose_hp") end
    local choice = room:askForChoice(player, choices, self.name, "#kaman_yuzhi-choice::"..data.from..":"..data.card:toLogString())
    if choice then
      self.cost_data = choice
      return true
    end
    return false
  end,

  on_use = function(self, event, target, player, data)
      local room = player.room
    -- 失去体力或体力上限
    if self.cost_data == "lose_hp" then
      room:loseHp(player, 1, self.name)
    else
      room:changeMaxHp(player, -1)
    end
    
    if player.dead then return end
    -- 取消目标
      table.insertIfNeed(data.nullifiedTargets, player.id)
    -- 摸两张牌
      room:drawCards(player, 2, self.name)
    -- 对来源使用杀
    local from = room:getPlayerById(data.from)
    if not from.dead then
      local slash = Fk:cloneCard("slash")
      slash.skillName = self.name
      room:useCard({
        from = player.id,
        tos = {{data.from}},
        card = slash,
        prohibitedCardNames = {"jink", "nullification"},  -- 此牌不可响应
      })
    end
  end,
}

zio:addSkill(kaman_shiting)
zio:addSkill(kaman_huisu)
zio:addSkill(kaman_fuhuo)
zio:addSkill(kaman_yuzhi)  -- 添加预知技能




local zio1 = General:new(extension, "kaman__zio1", "god", 4, 4)

-- 添加武将翻译
Fk:loadTranslationTable {
  ["kaman__zio1"] = "ZIO一阶",
  ["#kaman__zio1"] = "时间之王",
  ["designer:kaman__zio1"] = "DECADE",
  ["cv:kaman__zio1"] = "无",
  ["illustrator:kaman__zio1"] = "无",
}

local kaman_shikongjiayu = fk.CreateTriggerSkill{
  name = "kaman_shikongjiayu",
  events = {fk.GameStart, fk.Death},
  
  can_trigger = function(self, event, target, player, data)
    if event == fk.GameStart then
      return player:hasSkill(self)
    else
      return player:hasSkill(self) and target ~= player and target.dead
    end
  end,

  on_cost = function(self, event, target, player, data)
    if event == fk.GameStart then return true end
    return true
  end,

  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart then
      -- 初始化驾驭表
      local mark = {}
      local generals = table.clone(room.general_pile)
      for _ = 1, 3 do
        if #generals == 0 then break end
        local g = table.remove(generals, math.random(#generals))
        table.insert(mark, g)
        table.removeOne(room.general_pile, g)
      end
      room:setPlayerMark(player, "@&kaman_shikongjiayu", mark)
      room:sendLog{
        type = "#kaman_shikongjiayu_get",
        from = player.id,
        arg = #mark,
      }
    else
      -- 获得死亡角色的驾驭表
      local deadMark = {}
      if target.general then table.insert(deadMark, target.general) end
      if target.deputy then table.insert(deadMark, target.deputy) end
      
      if #deadMark > 0 then
        local mark = player:getTableMark("@&kaman_shikongjiayu") or {}
        for _, g in ipairs(deadMark) do
          table.insert(mark, g)
        end
        room:setPlayerMark(player, "@&kaman_shikongjiayu", mark)
        room:sendLog{
          type = "#kaman_shikongjiayu_gain",
          from = player.id,
          to = {target.id},
          arg = #deadMark,
        }
      end
    end
  end,
}

zio1:addSkill(kaman_shikongjiayu)

local kaman_zhuangjia = fk.CreateTriggerSkill{
  name = "kaman_zhuangjia",
  events = {fk.TurnStart, fk.EventPhaseStart},
  
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self.name) then return false end
    
    if event == fk.TurnStart then
      return target == player.room:getCurrent()
    else
      return target == player and player.phase == Player.Start
    end
  end,

  on_cost = function(self, event, target, player, data)
    local room = player.room
    local mark = player:getTableMark("@&kaman_shikongjiayu") or {}
    if #mark == 0 then return false end
    
    -- 让玩家选择一个驾驭表
    local generals = {}
    for _, g in ipairs(mark) do
      local general = Fk.generals[g]
      if general then
        table.insert(generals, g)
      end
    end

    if #generals == 0 then return false end

    -- 修改选择对话框的实现
    local result = room:askForChoice(player, generals, self.name)
    
    if result then
      self.cost_data = result
      return true
    end
    return false
  end,

  on_use = function(self, event, target, player, data)
    local room = player.room
    local general = Fk.generals[self.cost_data]
    if not general then return end
    
    -- 移除旧技能
    local old_mark = player:getMark("@kaman_zhuangjia_using")
    if old_mark ~= 0 then
      local old_general = Fk.generals[old_mark]
      if old_general then
        local skills = {}
        for _, skillName in ipairs(old_general:getSkillNameList()) do
          local s = Fk.skills[skillName]
          if not (s.lordSkill or s.switchSkillName or s.frequency > 3) and #s.attachedKingdom == 0 then
            table.insert(skills, skillName)
          end
        end
        if #skills > 0 then
          room:handleAddLoseSkills(player, "-" .. table.concat(skills, "|-"))
        end
      end
    end
    
    -- 获得新技能
    local skills = {}
    for _, skillName in ipairs(general:getSkillNameList()) do
      local s = Fk.skills[skillName]
      if not (s.lordSkill or s.switchSkillName or s.frequency > 3) and #s.attachedKingdom == 0 then
        table.insert(skills, skillName)
      end
    end
    
    if #skills > 0 then
      room:handleAddLoseSkills(player, table.concat(skills, "|"))
    end
    
    -- 记录当前使用的驾驭表
    room:setPlayerMark(player, "@kaman_zhuangjia_using", self.cost_data)
    
    -- 发送获得技能的提示
    room:sendLog{
      type = "#kaman_zhuangjia_get",
      from = player.id,
      arg = general.name,
    }
  end,
}

-- 添加技能翻译
Fk:loadTranslationTable{
  ["kaman_zhuangjia"] = "装甲时刻",
  [":kaman_zhuangjia"] = "每轮开始时或每名角色回合开始时，你可以选择获得一个驾驭表，视为拥有这个表的所有技能直到再次发动此技能。",
  ["#kaman_zhuangjia-choose"] = "装甲时刻：选择一个驾驭表获得其技能",
  ["#kaman_zhuangjia_get"] = "%from 获得了 %arg 的所有技能",
  ["@kaman_zhuangjia_using"] = "装甲时刻",
}
zio1:addSkill(kaman_zhuangjia)

local kaman_shikongjiayu_active = fk.CreateTriggerSkill{
  name = "#kaman_shikongjiayu_active",
  anim_type = "control",
  events = {fk.EventPhaseStart},
  
  can_trigger = function(self, event, target, player, data)
    -- 其他角色的出牌阶段开始时，且你有时空驾驭技能
    return target ~= player and target.phase == Player.Play and 
           player:hasSkill("kaman_shikongjiayu") and not target.dead
  end,

  on_cost = function(self, event, target, player, data)
    local room = player.room
    -- 询问目标是否发动技能
    return room:askForSkillInvoke(target, "kaman_shikongjiayu_give", data, "#kaman_shikongjiayu_give::"..player.id)
  end,

  on_use = function(self, event, target, player, data)
    local room = player.room
    
    -- 获取目标所有技能
    local skills = {}
    for _, s in ipairs(target.player_skills) do
      if s.visible and not s.name:startsWith("#") then
        table.insertIfNeed(skills, s.name)
      end
    end
    
    -- 失去所有技能
    if #skills > 0 then
      room:handleAddLoseSkills(target, "-" .. table.concat(skills, "|-"))
    end
    
    -- 获得驾驭表（只给有时空驾驭技能的玩家）
    local mark = player:getTableMark("@&kaman_shikongjiayu") or {}
    -- 如果是镜ZIO一阶,则只获得ZIO二阶的驾驭表
    if target.general == "kaman__zio1_jingxiang" then
      table.insert(mark, "kaman__zio")  -- 只添加ZIO二阶
    else
      if target.general then table.insert(mark, target.general) end
      if target.deputy then table.insert(mark, target.deputy) end
    end
    room:setPlayerMark(player, "@&kaman_shikongjiayu", mark)
    
    -- 发送日志
    room:sendLog{
      type = "#kaman_shikongjiayu_give",
      from = target.id,
      to = {player.id},
      arg = #mark,
    }
  end,
}

kaman_shikongjiayu:addRelatedSkill(kaman_shikongjiayu_active)

Fk:loadTranslationTable{
  ["kaman_shikongjiayu_give"] = "时空驾驭",
  ["#kaman_shikongjiayu_give"] = "你可以失去所有技能，令 %dest 获得你的驾驭表",
  ["#kaman_shikongjiayu_give_log"] = "%from 失去了所有技能，令 %to 获得了其驾驭表",
  ["kaman_shikongjiayu"] = "时空驾驭",
  [":kaman_shikongjiayu"] = "游戏开始时，你将三张武将牌置于武将牌上称作驾驭表。其他武将死亡后，你获得该角色驾驭表。其他角色回合开始时可以令你获得一个驾驭表。",
  ["#kaman_shikongjiayu_get"] = "%from 将 %arg 张武将牌置于武将牌上作为驾驭表",
  ["#kaman_shikongjiayu_gain"] = "%from 获得了 %to 的 %arg 张驾驭表",
  ["@&kaman_shikongjiayu"] = "驾驭表",
}

local kaman_cuanshi = fk.CreateActiveSkill{
  name = "kaman_cuanshi",
  anim_type = "control",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  
  card_num = 0,
  target_num = 1,
  
  target_filter = function(self, to_select, selected, selected_cards)
    local target = Fk:currentRoom():getPlayerById(to_select)
    -- 目标必须是其他角色且没有技能
    return to_select ~= Self.id and #target.player_skills == 0
  end,
  
  feasible = function(self, selected, selected_cards)
    return #selected == 1
  end,
  
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    
    -- 获取驾驭表
    local mark = player:getTableMark("@&kaman_shikongjiayu") or {}
    if #mark == 0 then return end
    
    -- 让玩家选择一个驾驭表
    local generals = {}
    for _, g in ipairs(mark) do
      local general = Fk.generals[g]
      if general then
        table.insert(generals, g)
      end
    end
    
    if #generals == 0 then return end
    
    -- 选择要失去的驾驭表
    local choice = room:askForChoice(player, generals, self.name)
    if not choice then return end
    
    -- 从驾驭表中移除选择的武将
    table.removeOne(mark, choice)
    room:setPlayerMark(player, "@&kaman_shikongjiayu", mark)
    
    -- 获取选中武将的技能
    local general = Fk.generals[choice]
    if not general then return end
    
    local skills = {}
    for _, skillName in ipairs(general:getSkillNameList()) do
      local s = Fk.skills[skillName]
      if not (s.lordSkill or s.switchSkillName or s.frequency > 3) and #s.attachedKingdom == 0 then
        table.insert(skills, skillName)
      end
    end
    
    -- 给予目标角色技能
    if #skills > 0 then
      room:handleAddLoseSkills(target, table.concat(skills, "|"))
      -- 发送获得技能的提示
      room:sendLog{
        type = "#kaman_cuanshi_give",
        from = player.id,
        to = {target.id},
        arg = general.name,
      }
    end
  end,
}

Fk:loadTranslationTable{
  ["kaman_cuanshi"] = "篡时",
  [":kaman_cuanshi"] = "出牌阶段限一次，你可以失去一个驾驭表，令一名没有技能的其他角色获得该驾驭表上的所有技能。",
  ["#kaman_cuanshi_give"] = "%from 失去了 %arg 的驾驭表，令 %to 获得其所有技能",
}

zio1:addSkill(kaman_cuanshi) 


local zio1_jingxiang = General:new(extension, "kaman__zio1_jingxiang", "god", 4, 4)
zio1_jingxiang.total_hidden = true
-- 添加武将翻译
Fk:loadTranslationTable {
  ["kaman__zio1_jingxiang"] = "镜ZIO一阶",
  ["#kaman__zio1_jingxiang"] = "镜像时间之王",
  ["designer:kaman__zio1_jingxiang"] = "DECADE",
  ["cv:kaman__zio1_jingxiang"] = "无",
  ["illustrator:kaman__zio1_jingxiang"] = "无",
}
zio1_jingxiang:addSkill(kaman_shikongjiayu)
zio1_jingxiang:addSkill(kaman_zhuangjia)
zio1_jingxiang:addSkill(kaman_cuanshi)

local kaman_shizhiwang = fk.CreateTriggerSkill{
  name = "kaman_shizhiwang",
  frequency = Skill.Compulsory,
  events = {fk.GameStart, fk.EventPhaseStart, fk.Death, fk.MarkChanged},
  
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self.name) then return false end
    
    if event == fk.Death then
      -- 当镜ZIO死亡时触发
      return target.general == "kaman__zio1_jingxiang"
    elseif event == fk.MarkChanged then
      -- 当驾驭表数量变化时触发
      return target == player and data.name == "@&kaman_shikongjiayu"
    end
    
    -- 检查场上是否已有镜ZIO一阶
    local has_mirror = false
    for _, p in ipairs(player.room.alive_players) do
      if p.general == "kaman__zio1_jingxiang" then
        has_mirror = true
        break
      end
    end
    
    if has_mirror then return false end
    
    return event == fk.GameStart or 
           (event == fk.EventPhaseStart and target == player and target.phase == Player.Start)
  end,

  on_use = function(self, event, target, player, data)
    local room = player.room
    
    if event == fk.Death then
      -- 获得ZIO二阶的驾驭表
      local mark = player:getTableMark("@&kaman_shikongjiayu") or {}
      table.insert(mark, "kaman__zio")  -- 只添加ZIO二阶
      room:setPlayerMark(player, "@&kaman_shikongjiayu", mark)
      -- 发送获得驾驭表的提示
      room:sendLog{
        type = "#kaman_shizhiwang_gain",
        from = player.id,
        to = {target.id},
      }
      return
    end
    
    -- 获取所有友方角色
    local friends = {}
    for _, p in ipairs(room.alive_players) do
      if p ~= player and p.role == player.role then
        table.insert(friends, p)
      end
    end
    
    if #friends == 0 then return end
    
    -- 随机选择一名友方角色
    local target = friends[math.random(1, #friends)]
    
    -- 获取目标所有技能
    local skills = {}
    for _, s in ipairs(target.player_skills) do
      if s.visible and not s.name:startsWith("#") then
        table.insertIfNeed(skills, s.name)
      end
    end
    
    -- 失去所有技能
    if #skills > 0 then
      room:handleAddLoseSkills(target, "-" .. table.concat(skills, "|-"))
    end
    
    -- 替换武将
    room:changeHero(target, "kaman__zio1_jingxiang", false, false)
    
    -- 为新的镜ZIO添加三个驾驭表
    local mark = {}
    local generals = table.clone(room.general_pile)
    for _ = 1, 3 do
      if #generals == 0 then break end
      local g = table.remove(generals, math.random(#generals))
      table.insert(mark, g)
      table.removeOne(room.general_pile, g)
    end
    room:setPlayerMark(target, "@&kaman_shikongjiayu", mark)
    
    -- 发送日志
    room:sendLog{
      type = "#kaman_shizhiwang",
      from = player.id,
      to = {target.id},
    }
    room:sendLog{
      type = "#kaman_shikongjiayu_get",
      from = target.id,
      arg = #mark,
    }
  end,
}

-- 添加出杀次数加成
local shizhiwang_mod = fk.CreateTargetModSkill{
  name = "#kaman_shizhiwang_mod",
  residue_func = function(self, player, skill, scope, card)
    if player:hasSkill("kaman_shizhiwang") and skill.trueName == "slash_skill" and scope == Player.HistoryPhase then
      local mark = player:getTableMark("@&kaman_shikongjiayu") or {}
      return #mark
    end
  end,
}

kaman_shizhiwang:addRelatedSkill(shizhiwang_mod)
zio1:addSkill(kaman_shizhiwang)
Fk:loadTranslationTable{
  ["kaman_shizhiwang"] = "时之王",
  [":kaman_shizhiwang"] = "锁定技，①游戏开始时或准备阶段开始时：若场上没有【镜ZIO一阶】则令一名随机友方角色失去所有技能并替换武将为【镜ZIO一阶】。②当【镜ZIO一阶】死亡时，你获得【ZIO二阶】的驾驭表。③你每有一个驾驭表，你出杀次数+1。",
  ["#kaman_shizhiwang"] = "%from 发动了【时之王】，将 %to 变为了镜ZIO一阶",
  ["#kaman_shizhiwang_gain"] = "%from 获得了 %to 的驾驭表",
  ["@shizhiwang_maxhp"] = "时之王体力",
}

-- 创建时间武装技能
local kaman_shijianzhuangbei = fk.CreateTriggerSkill{
  name = "kaman_shijianzhuangbei",
  frequency = Skill.Compulsory,
  events = {fk.DamageInflicted},
  
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self.name)
  end,
  
  on_use = function(self, event, target, player, data)
    data.damage = data.damage - 1
    if data.damage < 0 then data.damage = 0 end
  end,
}

-- 创建攻击范围加成
local shijianzhuangbei_distance = fk.CreateDistanceSkill{
  name = "#kaman_shijianzhuangbei_distance",
  correct_func = function(self, from, to)
    if from:hasSkill("kaman_shijianzhuangbei") then
      return -1
    end
  end,
}

kaman_shijianzhuangbei:addRelatedSkill(shijianzhuangbei_distance)

zio1:addSkill(kaman_shijianzhuangbei)
zio1_jingxiang:addSkill(kaman_shijianzhuangbei)

Fk:loadTranslationTable{
  ["kaman_shijianzhuangbei"] = "时间武装",
  [":kaman_shijianzhuangbei"] = "锁定技，①你的攻击距离+1，②你受到的伤害-1。",
}
            


return extension
